package com.nebula.salary.portal.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.nebula.salary.common.result.ServerResponse;
import com.nebula.salary.common.utils.MD5Util;
import com.nebula.salary.common.utils.SecurityUtil;
import com.nebula.salary.model.dto.AnswerDto;
import com.nebula.salary.model.pojo.Answer;
import com.nebula.salary.model.pojo.Question;
import com.nebula.salary.model.pojo.User;
import com.nebula.salary.portal.service.IAnswerService;
import com.nebula.salary.portal.service.IQuestionService;
import com.nebula.salary.portal.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户密保问题答案表 前端控制器
 * </p>
 *
 * @author codex
 * @since 2022-01-24
 */
@RestController
@RequestMapping("/answer")
@Api(tags = "安全问题答案")
public class AnswerController {
    
    @Autowired
    private IQuestionService questionService;
    
    @Autowired
    private IAnswerService answerService;
    
    @ApiOperation("获取当前用户设置的密保模板编号")
    @GetMapping
    public ServerResponse<List<Integer>> getUserQuestion() {
        List<Answer> answers = answerService.list(new QueryWrapper<Answer>().eq(Answer.USER_ID, SecurityUtil.getUserId()).orderByAsc(Answer.ID));
        List<Integer> result = Lists.newArrayList();
        answers.forEach(answer -> {
            result.add(answer.getQuestionId());
        });
        return ServerResponse.createBySuccess(result);
    }
    
    @ApiOperation("更新安全问题以及答案")
    @PutMapping
    public ServerResponse<String> updateAnswer(@RequestBody AnswerDto answerDto) {
        
        if (answerDto.getQuestion1() == null || Strings.isNullOrEmpty(answerDto.getAnswer1())
                || answerDto.getQuestion2() == null || Strings.isNullOrEmpty(answerDto.getAnswer2())
                || answerDto.getQuestion3() == null || Strings.isNullOrEmpty(answerDto.getAnswer3())) {
            return ServerResponse.createByErrorMessage("请正确输入数据");
        }
        
        return answerService.updateAnswer(answerDto)
                ? ServerResponse.createBySuccessMessage("更新安全问题成功")
                : ServerResponse.createByErrorMessage("更新安全问题失败");
    }
    
    @ApiOperation("获取问题模板")
    @GetMapping("/question")
    public ServerResponse<List<Map<String, Object>>> getQuestion() {
        List<Question> questions = questionService.list();
        List<Map<String, Object>> result = Lists.newArrayList();
        questions.forEach(question -> {
            Map<String, Object> map = Maps.newLinkedHashMap();
            map.put("questionId", question.getQuestionId());
            map.put("question", question.getQuestion());
            map.put("disabled", false);
            result.add(map);
        });
        
        return ServerResponse.createBySuccess(result);
    }
}

